define([ "app", "jquery", "md5", "ecb", "services/UserService",
		"resources/UserResource", "resources/AddYokiUserResource" ],
		function(app) {

			var deps = [ "$scope", "$state", "$window", "$modalInstance", "$location", "$stateParams", "UserService", "UserResource", "AddYokiUserResource" ];

			function controller($scope, $state, $window, $modalInstance, $location, $stateParams, UserService, UserResource, AddYokiUserResource) {
				
				
				$scope.saveUser = function() {

					var password =encrypt($scope.userVo.password, $scope.userVo.userName);
					$scope.userVo.password=password;
					if ($scope.userCreateForm.$invalid) {
						return false;
					}
					$scope.userCreateForm.$invalid = true;
					
					AddYokiUserResource.save($scope.userVo).success(function(data){
						$scope.userCreateForm.$invalid = false;
						if (data.userId) {
							$modalInstance.close($scope.userVo);
						}
		            });
					
				};

				$scope.close = function($scope,AddYokiUserResource) {
					$modalInstance.close();
				};
				
				/**
				 * 初始化下拉列表
				 *//*
				function initDate(AddYokiUserResource){
					
					AddYokiUserResource.queryOrgList(0).success(function(data){
						$scope.orgList=data;
		            });
					//$scope.selected=$scope.orgList[0].companyCode;
					//$scope.companyVo.companyCode=$scope.orgList[0].companyCode;
				}*/
			}
			
			/**
			 * 密码 encrypt 加密
			 */
			function encrypt(content,srcKey){
			      var keyLen = 8;
				  var srcKeyLen = srcKey.length;
				
				 if(srcKeyLen > keyLen){
					 srcKey = srcKey.substr(0,keyLen);
					
				 }else{
					 var paddLen = keyLen -  srcKeyLen;
					 for(var i=0;i<paddLen;i++){
						 srcKey += "0";
					 }
				 }
				
				 var key = CryptoJS.enc.Utf8.parse(srcKey);		
				 var encryptStr =  CryptoJS.DES.encrypt(content, key, {mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
				return encryptStr.toString();
			}
			
			controller.$inject = deps;
			return app.lazy.controller("AddUserController", controller);
		});
